package com.haidechizi.fastadmin.modules.sys.controller;


import com.haidechizi.fastadmin.common.annotation.OperateLog;
import com.haidechizi.fastadmin.common.base.AbstractController;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.modules.sys.entity.SysConfig;
import com.haidechizi.fastadmin.modules.sys.entity.SysDict;
import com.haidechizi.fastadmin.modules.sys.service.SysConfigService;
import com.haidechizi.fastadmin.modules.sys.service.SysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 系统配置信息
 */
@RestController
@RequestMapping("/sys/config")
public class SysConfigController extends AbstractController {
    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private SysDictService sysDictService;

    /**
     * 所有配置列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:config:list")
    public Response list(PageDto pageDto, SysConfig sysConfig) {
        PageInfo page = sysConfigService.queryPage(pageDto, sysConfig);

        return Response.ok().put("page", page);
    }


    /**
     * 配置信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:config:info")
    public Response info(@PathVariable("id") Long id) {
        SysConfig config = sysConfigService.selectById(id);

        return Response.ok().put("config", config);
    }

    /**
     * 保存配置
     */
    @OperateLog("保存配置")
    @RequestMapping("/save")
    @RequiresPermissions("sys:config:save")
    public Response save(@RequestBody SysConfig config) {
        ValidatorUtils.validateEntity(config);

        sysConfigService.save(config);

        return Response.ok();
    }

    /**
     * 修改配置
     */
    @OperateLog("修改配置")
    @RequestMapping("/update")
    @RequiresPermissions("sys:config:update")
    public Response update(@RequestBody SysConfig config) {
        ValidatorUtils.validateEntity(config);

        sysConfigService.update(config);

        return Response.ok();
    }

    /**
     * 删除配置
     */
    @OperateLog("删除配置")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:config:delete")
    public Response delete(@RequestBody Long[] ids) {
        sysConfigService.deleteBatch(ids);

        return Response.ok();
    }

    /**
     * 获取字典列表(初始化select、checkbox、radio)
     *
     * @return
     */
    @RequiresPermissions("sys:config:list")
    @RequestMapping(value = "dictConfig")
    public Response dictConfig() {
        List<SysDict> statusList = sysDictService.queryDictByType("sys_config_status");

        return Response.ok()
                .put("statusList", statusList)
                ;
    }


    /**
     * 获取字典Map(初始化字典数据，将code转成value)
     *
     * @return
     */
    @RequiresPermissions("sys:config:list")
    @RequestMapping(value = "dictMap")
    public Response dictMap() {
        Map<String, String> statusMap = sysDictService.queryDictMapByType("sys_config_status");
        return Response.ok()
                .put("statusMap", statusMap)
                ;
    }

}
